// @flow

import * as React from 'react';
import { connect } from 'react-redux';
import { selectNav } from '../actions/layout.action';

function mapDispatchToProps(dispatch: any){
	return {
		selectNav: key => dispatch(selectNav(key))
	}
}
function mapStateToProps(){
	return {}
}
type Props = {
	selectNav: Function
}
type States = {
	user: Object
}

export default function SelectNavHOC(Comp:Object): Object{
	@connect(mapStateToProps,mapDispatchToProps)
	class SelectNavHOC extends React.Component<Props,States> {
		constructor(props: Props){
			super(props);
			this.state={
				user:{}
			}
		}
		componentWillMount(){
			let user: ?string = localStorage.getItem('e_book_user');
			if (user) {
				user = JSON.parse(user);
				this.setState({user});
			}else{
				// localStorage需设置过期时间
				window.location.href='/?'+(new Date().getTime())+'#/login';
			}
		}
		componentDidMount(){
			console.log(this.tar.className)
		}
		render(): React.Node {
			return <Comp ref={ref=>this.tar=ref} {...this.props} selectNav={this.props.selectNav} user={this.state.user} />
		}
	}
	return SelectNavHOC;
}